package com.sercomm.ott.mediaplayer;

import android.app.Service;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.net.wifi.WifiManager;
import android.os.IBinder;
import android.util.Log;
import android.widget.Toast;

/**
 * Created by davis on 17-9-12.
 */

public class WifiService extends Service {

    public static final String TAG = "WifiService";
    private IntentFilter mWifiStateFilter=null;
    WifiManager wifiManager = null;
    private long startTime =0;
    public static long wifiUpTime = 0;
    private final String BSSID = "00:00:00:00:00:00";
    WifiTimeThread wifiTimeThread = null;
    boolean wifi_thread_flag = true;
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        wifiManager = (WifiManager)getSystemService(WIFI_SERVICE);
        Log.d(TAG,"-----davis is wifi enabled "+wifiManager.isWifiEnabled());

        mWifiStateFilter = new IntentFilter(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.RSSI_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        mWifiStateFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        mWifiStateFilter.addAction(WifiManager.SCAN_RESULTS_AVAILABLE_ACTION);
        registerReceiver(mWifiStateReceiver,mWifiStateFilter);

        wifiTimeThread =new WifiTimeThread();
        wifiTimeThread.start();

        super.onCreate();
        Log.d(TAG, "-------davis wifi service onCreate");
    }

    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.d(TAG, "-----davis in wifi service onStartCommand");
        Log.d(TAG, "-----davis WifiService:" + this);
        wifi_thread_flag=true;
        return START_STICKY;
    }

    @Override
    public void onDestroy() {
        if(wifi_thread_flag){
            wifi_thread_flag = false;
        }
        unregisterReceiver(mWifiStateReceiver);
        super.onDestroy();
        Log.d(TAG, "--------davis wifi service in onDestroy");

    }

    private final BroadcastReceiver mWifiStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            if(intent.getAction().equals(WifiManager.WIFI_STATE_CHANGED_ACTION)){
                wifiUpTime=0;
                if(!wifiManager.isWifiEnabled()){
                    startTime = System.currentTimeMillis();
                }
                Log.d(TAG,"-----davis wifi state changed action");
            }

        }
    };
    public class WifiTimeThread extends Thread{
        private String current_bssid=null;
        private String old_bssid = null;
        private long currentTime = 0;

        @Override
        public void run(){
            while (wifi_thread_flag) {
                try {
                    if (wifiManager.isWifiEnabled()) {
                        old_bssid = wifiManager.getConnectionInfo().getBSSID();
                        if((old_bssid!=null && old_bssid.equals(BSSID)) || wifiManager.getConnectionInfo().getLinkSpeed()<0){
                            Thread.currentThread().sleep(1000);
                            continue;
                        }
                        startTime=System.currentTimeMillis();
                    }
                    while (wifi_thread_flag) {
                        Thread.currentThread().sleep(1000);
                        if (wifiManager.isWifiEnabled() ) {
                            current_bssid = wifiManager.getConnectionInfo().getBSSID();
                            if (current_bssid!=null && current_bssid.equals(old_bssid) && wifiManager.getConnectionInfo().getLinkSpeed()>0) {
                                currentTime = System.currentTimeMillis();
                                wifiUpTime = currentTime - startTime;
                            }else {
                                break;
                            }
                        }
                    }
                }catch (Exception e){
                    e.printStackTrace();
                }
            }

        }
    }



}
